package com.hsy.crm.web.domain.cm;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.LongSerializationPolicy;

public class Response {

    public static String body(Object obj) {
        String json = toJson(obj);
        if( obj instanceof Unified  || obj instanceof PageResult ){
            return json;
        }else{
            throw new RuntimeException("action return type error:"+json);
        }
    }

    public static String iframe(Object obj){
        String json = toJson(obj);
        return "<script>parent.streamHandle("+json+");</script>";
    }

    private static Gson GSON = null;

    public static String toJson(Object obj) {
        return getGson().toJson(obj);
    }

    private static Gson getGson() {
        if(GSON==null){

            GsonBuilder builder = new GsonBuilder();
            builder.setLongSerializationPolicy(LongSerializationPolicy.STRING);

            GSON = builder.create();
        }
        return GSON;
    }

}
